package com.ymatou.iapi.platformdiscounts.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.platformdiscounts.parameter.AddBean;
import com.ymatou.iapi.platformdiscounts.parameter.CalculateBean;
import com.ymatou.iapi.platformdiscounts.parameter.DiscountInfo;
import com.ymatou.iapi.platformdiscounts.parameter.ProductDto;
import com.ymatou.iapi.platformdiscounts.parameter.QueryBean;
import com.ymatou.iapi.platformdiscounts.parameter.DiscountInfo.ProductDiscountsInfo;
import com.ymatou.iapi.platformdiscounts.service.AddCall;
import com.ymatou.iapi.platformdiscounts.service.CalculateCall;
import com.ymatou.iapi.platformdiscounts.service.QueryCall;
import com.ymatou.iapi.proditem.service.GetProductInfoByProductIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DiscountsCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.discount_info;
import com.ymttest.database.sqlwapper.discount_infoWapper;
import com.ymttest.database.sqlwapper.discount_qualificationWapper;
/**
*查询优惠信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_query{
	private static QueryBean bean;
	private static QueryCall call;
	private static DiscountsCallService callService=new  DiscountsCallService();
	private discount_qualificationWapper dqWapper=new discount_qualificationWapper();
	private SellerProductCallService sellerProuctService=new SellerProductCallService();
	private discount_infoWapper infoWapper=new discount_infoWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询优惠信息");
	}

	@Before
	public void caseUp() {
		bean=new QueryBean();
		call=new QueryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_query_001() {
		Logger.start(true,"正例-查询优惠信息");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Logger.comment("############计新增优惠资格结束#################");
			
			bean.setUserId(calculateBean.getUserId());
			List<String>  products=new ArrayList();
			products.add(calculateBean.getProducts().get(0).getProductId());
			bean.setProductIds(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_query_002() {
		Logger.start(true,"正例-查询优惠信息-商品个数为多个");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			//新增优惠资格1
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			//新增优惠资格2
			AddBean addBean2=callService.setAdd();
			addBean2.setUserId(addBean.getUserId());
			addBean2.setProductId(addBean.getProductId().substring(0, addBean.getProductId().length()-1)+"1");
			AddCall addCall2=callService.callAdd(addBean2);
			Logger.comment("############新增优惠资格结束#################");
			//计算优惠资格开始
			Logger.comment("############计算优惠资格开始#################");
			CalculateBean cbean=new CalculateBean();
			cbean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			ProductDto dto2=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			
			dto2.setProductId(addBean2.getProductId());
			dto2.setPrice(new BigDecimal(price));	
			dto2.setsecondCategoryId(obj.getInt("CategoryId"));
			
			products.add(dto);
			products.add(dto2);
			
			cbean.setProducts(products);
			cbean.setAppId("11");
			cbean.setRequestId("22");
			callService.callCalculated(cbean);
			Logger.comment("############计算优惠资格结束#################");
			
			Thread.sleep(10000);
			bean.setUserId(cbean.getUserId());
			List<String>  productList=new ArrayList();
			productList.add(cbean.getProducts().get(0).getProductId());
			productList.add(cbean.getProducts().get(1).getProductId());
			bean.setProductIds(productList);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_query_003() {
		Logger.start(false,"反例-查询优惠信息-userId为空");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Logger.comment("############计新增优惠资格结束#################");
			
			//bean.setUserId(calculateBean.getUserId());
			List<String>  products=new ArrayList();
			products.add(calculateBean.getProducts().get(0).getProductId());
			bean.setProductIds(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals(true, call.getErrorMsg().contains("userId must"), "验证返回ErrorCode");
			Logger.verifyEquals("1", call.getErrorCode(), "验证返回ErrorMsg");
			Logger.verifyEquals("null", call.getProductDiscounts(), "验证返回ErrorMsg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_query_004() {
		Logger.start(false,"反例-查询优惠信息-productIds为空");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Logger.comment("############计新增优惠资格结束#################");
			
			bean.setUserId(calculateBean.getUserId());
			List<String>  products=new ArrayList();
//			products.add(calculateBean.getProducts().get(0).getProductId());
			bean.setProductIds(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals(true, call.getErrorMsg().contains("productIds must"), "验证返回ErrorCode");
			Logger.verifyEquals("1", call.getErrorCode(), "验证返回ErrorMsg");
			Logger.verifyEquals("null", call.getProductDiscounts(), "验证返回ErrorMsg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn()
	{
		Logger.verifyEquals("0", call.getErrorCode(), "验证返回ErrorCode");
		Logger.verifyEquals("null", call.getErrorMsg(), "验证返回ErrorMsg");
		//验证数据库
		ProductDiscountsInfo respInfos=call.getCallData();
		Logger.verifyEquals(bean.getProductIds().size(), respInfos.getProductDiscounts().size(), "验证返回长度与请求商品长度一致");
		for(int i=respInfos.getProductDiscounts().size()-1;i<0;i++)
		{
			DiscountInfo respInfo=respInfos.getProductDiscounts().get(i);
			discount_info record=new discount_info();
			record.setUserId(bean.getUserId());
			record.setProductId(bean.getProductIds().get(i));
			discount_info DBinfo=infoWapper.selectTop1(record);
			
			Logger.verifyEquals(respInfo.getProductId(), DBinfo.getProductId(), "验证第"+(i+1)+"条数据库discount_info 字段ProductId");
			Logger.verifyEquals(respInfo.getDiscountAmount().stripTrailingZeros().toPlainString(), DBinfo.getSingleDiscountAmount().stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条数据库discount_info 字段DiscountTotalAmount");
			Logger.verifyEquals(true,calLastedTime(DBinfo.getStartTime(),respInfo.getStartTime())<10, "验证第"+(i+1)+"条数据库discount_info 字段StartTime");
			Logger.verifyEquals(true,calLastedTime(DBinfo.getExpireTime(),respInfo.getExpireTime())<10 , "验证第"+(i+1)+"条数据库discount_info 字段ExpireTime");
		}
	}
	//计算日期误差，返回的StartTime、ExpireTime是直接从计算服务中获取，数据库的字段后插入，所以存在日期误差
	public  int calLastedTime(Date startDate,Date endDate) {
		  long a = endDate.getTime();
		  long b = startDate.getTime();
		  int c = (int)((a - b) / 1000);
		  return c;
	}
}